from re import T
import torch
import numpy as np

xy = np.loadtxt('./dataset/diabetes.csv',delimiter=',',dtype=np.float32)

x_data = torch.from_numpy(xy[:-1,:-1])
y_data = torch.from_numpy(xy[:-1,[-1]])

test_data = torch.from_numpy(xy[[-1],:-1])
pred_test = torch.from_numpy(xy[[-1],[-1]])

class Model(torch.nn.Module):
    def __init__(self) -> None:
        super(Model,self).__init__()
        self.linear1 = torch.nn.Linear(8,6)
        self.linear2 = torch.nn.Linear(6,4)
        self.linear3 = torch.nn.Linear(4,1)
        self.sigmoid = torch.nn.Sigmoid()

    def forward(self,x):
        x = self.sigmoid(self.linear1(x))
        x = self.sigmoid(self.linear2(x))
        x = self.sigmoid(self.linear3(x))
        return x

model = Model()

criterion = torch.nn.BCELoss(size_average=True)

optimizer = torch.optim.SGD(model.parameters(),lr=0.1)

for epoch in range(1000):
    y_pred = model(x_data)
    loss = criterion(y_pred,y_data)
    print(epoch,loss.item())

    optimizer.zero_grad()
    loss.backward()

    optimizer.step()

print('test_pred=',model(test_data).item())
print('infact_pred=',pred_test.item())

    
